package com.qianli.user.application;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Response;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.qianli.common.enums.UserAuthTypeEnum;
import com.qianli.user.domain.model.UserAccessModel;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.account.UserAccessRO;
import com.qianli.user.ro.auth.UserAuthInfoRO;
import com.qianli.user.ro.base.UserBaseInfoRO;
import com.qianli.user.ro.info.UserInfoRO;
import com.qianli.user.ro.query.UserInfoQueryRO;
import com.qianli.user.ro.social.UserSocialRO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * name:用户资料信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 20:57
 * version: v1.0.0
 **/
@Service
public class UserInfoApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoApplication.class);

    @Autowired
    private UserBaseInfoApplication userBaseInfoApplication;

    @Autowired
    private UserAuthApplication userAuthApplication;

    @Autowired
    private UserSocialApplication userSocialApplication;

    @Autowired
    private UserAccessApplication userAccessApplication;
    /**
     * 用户资料查询
     * @param queryRO
     * @return
     */
    public Response<UserInfoRO> getUserInfo(UserInfoQueryRO queryRO) {
        //查询用户账号信息
        UserAccessModel accessModel = new UserAccessModel();
        accessModel.setUserCode(queryRO.getUserCode());
        Response<UserAccessRO> uaResponse = userAccessApplication.queryUserAccess(accessModel);
        if(!uaResponse.isSuccess()){
            LOGGER.info("UserInfoApplication.getUserInfo query user access error:"+JSON.toJSONString(uaResponse));
            return Response.error(UserStateCode.getEnumByType(uaResponse.getCode()));
        }
        UserInfoRO userInfoRO = new UserInfoRO();
        List<UserAuthTypeEnum> userInfoTypeEnums = queryRO.getUserInfoTypeEnums();
        List<Integer> userModelTypes = convertTOModelType(userInfoTypeEnums);
        for(Integer type:userModelTypes){
            switch (type){
                case 1:
                    //查询认证信息
                    Response<UserAuthInfoRO> uauthResponse = userAuthApplication.load(queryRO.getUserCode());
                    if(uauthResponse.isSuccess()){
                        userInfoRO.setUserCreditAuth(uauthResponse.getData());
                    }
                    break;
                case 2:
                    //查询用户基本信息
                    Response<UserBaseInfoRO> ubmReponse = userBaseInfoApplication.load(queryRO.getUserCode());
                    if(ubmReponse.isSuccess()){
                        UserBaseInfoRO userBaseInfoRO = ubmReponse.getData();
                        userBaseInfoRO.setUser(uaResponse.getData());
                        userInfoRO.setUserBaseInfo(ubmReponse.getData());
                    }
                    break;
                case 3:
                    //查询社交信息
                    Response<UserSocialRO> usReponse = userSocialApplication.load(queryRO.getUserCode());
                    if(usReponse.isSuccess()){
                        UserSocialRO userSocial = usReponse.getData();
                        userInfoRO.setUserSocial(userSocial);
                    }
                    break;
                default:break;
            }
        }
        return Response.ok(userInfoRO);
    }

    /**
     * 类型转换
     * @param userInfoTypeEnums
     * @return
     */
    private List<Integer> convertTOModelType(List<UserAuthTypeEnum> userInfoTypeEnums) {
        List<UserAuthTypeEnum> authTypes = Lists.newArrayList(UserAuthTypeEnum.ZM,UserAuthTypeEnum.BANK_CARD,
                UserAuthTypeEnum.FACE_RECOGNITION,UserAuthTypeEnum.CARRIER,UserAuthTypeEnum.ALIPAY,
                UserAuthTypeEnum.CREDIT_CARD,UserAuthTypeEnum.TAOBAO,UserAuthTypeEnum.JD,
                UserAuthTypeEnum.SOCIAL_SECURITY,UserAuthTypeEnum.PROVIDENT_FUND,UserAuthTypeEnum.XUEXIN);
        List<UserAuthTypeEnum> infoTypes = Lists.newArrayList(UserAuthTypeEnum.USER_INFO);

        List<UserAuthTypeEnum> socialTypes = Lists.newArrayList(UserAuthTypeEnum.USER_INFO);

        Set<Integer> types = Sets.newHashSet();
        for(UserAuthTypeEnum typeEnum:userInfoTypeEnums){
            if(authTypes.contains(typeEnum)){
                types.add(1);
            }
            if(infoTypes.contains(typeEnum)){
                types.add(2);
            }
            if(socialTypes.contains(typeEnum)){
                types.add(3);
            }
        }
        return Lists.newArrayList(types);
    }
}
